package org.particles.gui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

/**
 * initializes simulation, rerun it.
 */
public class InitSimulationAction extends AbstractSimAction {
	private static final long serialVersionUID = 2556957682100250778L;
	private SimCtrlPanel controlPanel;
	
	public InitSimulationAction() {
		super();
	}
	
	/**
	 * must be called after all properties 
	 * (worldPanel, task, commonService, simulation) are set.
	 */
	public void initAction() {
		putValue(SHORT_DESCRIPTION, "ReRun simulation");
		putValue(MNEMONIC_KEY, KeyEvent.VK_R);
		putValue(SMALL_ICON, new ImageIcon(getCommonService().findImageUrlByName("Refresh16")));
		putValue(LARGE_ICON_KEY, new ImageIcon(getCommonService().findImageUrlByName("Refresh24")));
		putValue(NAME, "ReRun");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(this.controlPanel.inputErrorsAvailable()) {
			return;
		}
		final boolean running = getWorldTimeTask().isRunning();
		getWorldTimeTask().setRunning(false);
		GuiUtil.applyParametersToWorld(controlPanel);
		getWorldPanel().setWorld(controlPanel.getCurrentSimulation().createWorld());
		controlPanel.getCurrentSimulation().clearStatistic();
		controlPanel.repaintStatistics();
		getWorldPanel().revalidate();
		getWorldPanel().repaint();
		getWorldTimeTask().setRunning(running);
	}

	public void setControlPanel(SimCtrlPanel controlPanel) {
		this.controlPanel = controlPanel;
	}

}
